//
//  SendMsgViewController.m
//  XMPPHelper
//
//  Created by 蔡成汉 on 15/5/10.
//  Copyright (c) 2015年 JW. All rights reserved.
//

#import "SendMsgViewController.h"
#import "XMPPHelper.h"

@interface SendMsgViewController ()<UITextFieldDelegate>
{
    //收消息的label
    UILabel *msgLabel;
    
    //目标用户的用户名
    UITextField *userNameTextField;
    
    //消息输入框 - 指文本类消息
    UITextField *contentTextField;
}
@end

@implementation SendMsgViewController
@synthesize currentUserName;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"发送消息";
    
    //msgLabel
    msgLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)];
    msgLabel.backgroundColor = [UIColor clearColor];
    msgLabel.textColor = [UIColor redColor];
    msgLabel.textAlignment = NSTextAlignmentCenter;
    msgLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:msgLabel];
    
    //目标用户名
    userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 100, 150, 200, 30)];
    userNameTextField.delegate = self;
    userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
    userNameTextField.placeholder = @"用户名";
    userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    userNameTextField.returnKeyType = UIReturnKeyNext;
    [userNameTextField becomeFirstResponder];
    [self.view addSubview:userNameTextField];
    
    //内容输入框
    contentTextField = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 100, 200, 200, 30)];
    contentTextField.delegate = self;
    contentTextField.borderStyle = UITextBorderStyleRoundedRect;
    contentTextField.placeholder = @"内容";
    contentTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    contentTextField.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:contentTextField];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame =CGRectMake((self.view.frame.size.width/2 - 100), 250, 200, 30);
    [button setTitle:@"发送" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(buttonIsTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

-(void)buttonIsTouch:(UIButton *)paramSender
{
    Message *message = [[Message alloc]init];
    message.fromId = currentUserName;
    message.toId = userNameTextField.text;
    TextMessage *tpTextMessage = [[TextMessage alloc]init];
    tpTextMessage.text = contentTextField.text;
    message.content = tpTextMessage;
    message.type = FriendsMessage;
    message.messageType = MessageTypeForText;
    
    XMPPHelper *myXMPPHeleper = [XMPPHelper myXMPPHelper];
    [myXMPPHeleper sendMessage:message result:^(Message *message, MessageSendType sendType) {
        if (sendType == MessageIsSending)
        {
            NSLog(@"消息发送中");
        }
        else if (sendType == MessageSendSuccess)
        {
            NSLog(@"消息发送成功");
        }
        else
        {
            NSLog(@"消息发送失败");
        }
    }];
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == userNameTextField)
    {
        [contentTextField becomeFirstResponder];
        [userNameTextField resignFirstResponder];
    }
    else
    {
        [textField resignFirstResponder];
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
